const std = @import("std");
//部分注释来自于默认项目构建文件的注释

pub fn build(b: *std.Build) !void {
    //设置编译目标和设置为标准目标和设置
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    //设置导入raylib库的zig版本
    var ray = b.dependency("raylib", .{
        .target = target,
        .optimize = optimize
    });

    //获得编译结果，存为常量
    const raylib_clib = ray.artifact("raylib");
    
    //创建可执行文件
    const exe = b.addExecutable(.{
        .name = "ray-zigb",
        .target = target,
        .optimize = optimize,
    });

    //设置要编译的C源文件
    var cfiles = std.ArrayList([] const u8).init(b.allocator);
    defer cfiles.deinit();
    try cfiles.appendSlice(&[_][]const u8{
        "src/main.c",
    });

    //设置C编译参数
    var cflags = std.ArrayList([]const u8).init(b.allocator);
    defer cflags.deinit();
    try cflags.appendSlice(&[_][]const u8{
        "-std=c99",
    });

    //添加文件和参数
    exe.addCSourceFiles(.{
        .files = cfiles.items,
        .flags = cflags.items,
    });

    //添加头文件目录
    exe.addIncludePath(ray.path("src"));

    //链接C库和raylib库
    exe.linkLibC();
    exe.linkLibrary(raylib_clib);

    //安装可执行文件到输出目录
    b.installArtifact(exe);

    //设置运行目标为当前的可执行文件
    const run_cmd = b.addRunArtifact(exe);
    //运行步骤依赖于当前构建完成安装步骤
    run_cmd.step.dependOn(b.getInstallStep());

    //输入运行参数
    //例如：
    //zig build run -- args1
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    //设置运行参数的提示
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
